'RPGXML
'Creates XML version of OHRRPGs to use with MHRPGCE

Type RPGXML

	
	Method CreateXML(filepath:String,status:TGadget=Null)
	
		Local rpgfile:xmlDocument = New xmlDocument 'xmlDocument.Create()
		
		Local root:xmlNode = rpgfile.Root()
		If root = Null Then 
			Notify("Could not create RPG file")
			Return False
		EndIf
		
		root.Name = "MHRPG"
		root.Attribute("version").value = "0.5"
		root.Attribute("gname").value = "RPG-GAME"


		Local data_node:xmlNode = root.AddNode("data")
		
		Local mnum:Int = 0
		Local general_node:xmlNode = data_node.AddNode("general")
			general_node.Attribute("count").value = Gen.Length
			For Local i:Int = 0 To Gen.Length-1
			
				Local entry_node:xmlNode = general_node.AddNode("dat")
				entry_node.Attribute("id").value = i
				entry_node.Attribute("val").value = Gen[i]
				
			
			Next
		
		
		Local maps_node:xmlNode = data_node.AddNode("maps")
		
		maps_node.Attribute("count").value = CountList(Maps)
		For Local m:MapeditState = EachIn Maps
		
			Local map_node:xmlNode = maps_node.AddNode("map")
			map_node.Attribute("id").value = mnum
			map_node.Attribute("name").value = Replace(m.mapname,"&apos;","'")
			'Save as Tiled format
			map_node.Attribute("source").value = "maps/map_"+mnum+".xml"
			
			'Make doors - probably won't work on tiled so we need to create some data
			
			Local npcs_node:xmlNode = map_node.AddNode("npcs")
			npcs_node.Attribute("count").value = m.numnpcs
			
				For Local i:Int = 0 To m.numnpcs-1
					If m.npc_inst[i].id <> 0 Then
					
					Local npc_node:xmlNode = npcs_node.AddNode("npc")
						npc_node.Attribute("x").value = m.npc_inst[i].x
						npc_node.Attribute("y").value = m.npc_inst[i].y
						npc_node.Attribute("id").value = m.npc_inst[i].id
						npc_node.Attribute("dir").value = m.npc_inst[i].dir
						npc_node.Attribute("frame").value = m.npc_inst[i].frame
						'For Local j:Int = 0 To m.npc_def.Length-1
						
						npc_node.Attribute("pic").value = m.npc_def[m.npc_inst[i].id-1].valdata[0]
						If m.npc_def[m.npc_inst[i].id-1].valdata[1] = 65535 Then
							'get default palette
							npc_node.Attribute("pal").value = WalkaboutState.sets[m.npc_def[m.npc_inst[i].id-1].valdata[0]].defpal
						Else
							npc_node.Attribute("pal").value = m.npc_def[m.npc_inst[i].id-1].valdata[1]
						EndIf
						npc_node.Attribute("mt").value = m.npc_def[m.npc_inst[i].id-1].valdata[2]
						npc_node.Attribute("spd").value = m.npc_def[m.npc_inst[i].id-1].valdata[3]
						npc_node.Attribute("tb").value = m.npc_def[m.npc_inst[i].id-1].valdata[4]
						npc_node.Attribute("ft").value = m.npc_def[m.npc_inst[i].id-1].valdata[5]
						npc_node.Attribute("itm").value = m.npc_def[m.npc_inst[i].id-1].valdata[6]
						npc_node.Attribute("pt").value = m.npc_def[m.npc_inst[i].id-1].valdata[7]
						npc_node.Attribute("act").value = m.npc_def[m.npc_inst[i].id-1].valdata[8]
						npc_node.Attribute("tag1").value = m.npc_def[m.npc_inst[i].id-1].valdata[9]
						npc_node.Attribute("tag2").value = m.npc_def[m.npc_inst[i].id-1].valdata[10]
						npc_node.Attribute("utg").value = m.npc_def[m.npc_inst[i].id-1].valdata[11]
						npc_node.Attribute("scr").value = m.npc_def[m.npc_inst[i].id-1].valdata[12]
						npc_node.Attribute("stg").value = m.npc_def[m.npc_inst[i].id-1].valdata[13]
						npc_node.Attribute("veh").value = m.npc_def[m.npc_inst[i].id-1].valdata[14]
						npc_node.Attribute("dz").value = m.npc_def[m.npc_inst[i].id-1].valdata[15]
						npc_node.Attribute("dwz").value = m.npc_def[m.npc_inst[i].id-1].valdata[16]
					EndIf
					'Next
					
				
				Next
			
			
			
			Local doors_node:xmlNode = map_node.AddNode("doors")
			doors_node.Attribute("count").value = m.numdoors
				'wish i didn't have to create non existent doors....
				'wish granted bwhahaha
				For Local i:Int = 0 To m.numdoors-1
					Local door_node:xmlNode = doors_node.AddNode("door")
					door_node.Attribute("x").value = m.doors[i].x
					door_node.Attribute("y").value = m.doors[i].y
					door_node.Attribute("bits").value = m.doors[i].bits[0]
				Next

			Local doorlinks_node:xmlNode = map_node.AddNode("doorlinks")
			doorlinks_node.Attribute("count").value = m.numlinks
				'wish i didn't have to create non existent doors....
				'wish granted bwhahaha
				For Local i:Int = 0 To m.numlinks-1
					Local doorlink_node:xmlNode = doorlinks_node.AddNode("doorlink")
					doorlink_node.Attribute("source").value = m.link[i].source
					doorlink_node.Attribute("dest").value = m.link[i].dest
					doorlink_node.Attribute("dest_map").value = m.link[i].dest_map
					doorlink_node.Attribute("tag1").value = m.link[i].tag1
					doorlink_node.Attribute("tag2").value = m.link[i].tag2
					
					
					'Field source:Int, dest:Int, dest_map:Int, tag1:Int, tag2:Int

				Next
			
			
			Local mapdoc:xmlDocument = New xmlDocument
			
			Local maproot:xmlNode = mapdoc.Root()
			maproot.name = "map"
			maproot.Attribute("version").value = "1.0"
			maproot.Attribute("orientation").value ="orthogonal" 
			maproot.Attribute("width").value = m.map[0].wide
			maproot.Attribute("height").value = m.map[0].high
			If TileSState.Tileset[m.tilesets[0]] = Null Then
				maproot.Attribute("tilewidth").value = TileSState.Tileset[0].wide'spr.w
				maproot.Attribute("tileheight").value = TileSState.Tileset[0].high'spr.w
			Else
				maproot.Attribute("tilewidth").value = TileSState.Tileset[m.tilesets[0]].wide'spr.w
				maproot.Attribute("tileheight").value = TileSState.Tileset[m.tilesets[0]].high'spr.w			
			EndIf
			
			Local tsetslist:TList = New TList
			Local haveset:Byte = False
			For Local i:Int = 0 To m.tilesets.Length-1
				haveset = False
				For Local a:String = EachIn tsetslist
				
					If a = ""+m.tilesets[i] Then
						haveset = True
					EndIf
				
				Next
				If haveset = False Then 
					ListAddLast(tsetslist,""+m.tilesets[i])
				EndIf
			
			Next
			Local i:Int
			Local gidt:Int[TileSState.Tileset.Length]
			For Local a:String = EachIn tsetslist
				gidt[Int(a)] = (i*((TileSState.Tileset[Int(a)].spr.w/TileSState.Tileset[Int(a)].wide)*(TileSState.Tileset[Int(a)].spr.h/TileSState.Tileset[Int(a)].high)))+1
				'(i*((TileSState.Tileset[Int(a)].spr.w/TileSState.Tileset[Int(a)].wide)*(TileSState.Tileset[Int(a)].spr.h/TileSState.Tileset[Int(a)].high)))+1
				Local tilesetnode:xmlNode = maproot.AddNode("tileset")
				tilesetnode.Attribute("firstgid").value = (i*((TileSState.Tileset[Int(a)].spr.w/TileSState.Tileset[Int(a)].wide)*(TileSState.Tileset[Int(a)].spr.h/TileSState.Tileset[Int(a)].high)))+1
				tilesetnode.Attribute("name").value = a
				tilesetnode.Attribute("tilewidth").value = TileSState.Tileset[Int(a)].wide'spr.w
				tilesetnode.Attribute("tileheight").value = TileSState.Tileset[Int(a)].high'spr.w
				
				Local imgnode:xmlNode = tilesetnode.AddNode("image")
				imgnode.Attribute("source").value = "tileset_"+a+".png"
				imgnode.Attribute("width").value = TileSState.Tileset[Int(a)].spr.w
				imgnode.Attribute("height").value = TileSState.Tileset[Int(a)].spr.h
								
				i = i + 1
			Next	

			For Local l:Int = 0 To m.map.Length-1
				Local layernode:xmlNode = maproot.AddNode("layer")
				layernode.Attribute("name").value = l
				layernode.Attribute("width").value = m.map[l].wide
				layernode.Attribute("height").value = m.map[l].high
				
				Local mapdatanode:xmlNode = layernode.AddNode("data")
				mapdatanode.Attribute("encoding").value = "csv"
				'Local gid:Int = (l*((TileSState.Tileset[m.tilesets[l]].spr.w/TileSState.Tileset[m.tilesets[l]].wide)*(TileSState.Tileset[m.tilesets[l]].spr.h/TileSState.Tileset[m.tilesets[l]].high)))+1
				Local gid:Int = gidt[m.tilesets[l]]
				Local wide:Int = 0
				Local mapval:String
				For Local tm:Int = 0 To m.map[l].data.Length-1
					
								 	
					'somehow i get an array index out of range in monkey on some maps
					If tm <> m.map[l].data.Length-1 Then
						If l > 0 Then
							'this gets a little weird for Tiled, don't draw the first tile on above layers..
							If m.map[l].data[tm] = 0 Then
								mapval = mapval +"0,"
							Else
								mapval = mapval +Int(gid+m.map[l].data[tm])+","	
							EndIf
						Else
							mapval = mapval +Int(gid+m.map[l].data[tm])+","	
						EndIf
					Else
						If l > 0 Then
							'this gets a little weird for Tiled, don't draw the first tile on above layers..
							If m.map[l].data[tm] = 0 Then
								mapval = mapval +"0"
							Else
								mapval = mapval +Int(gid+m.map[l].data[tm])
							EndIf
						Else
							mapval = mapval +Int(gid+m.map[l].data[tm])
						EndIf
					
							
					
					EndIf
				Next
				mapdatanode.value = mapval
			Next
			'DO WALL MAPS	
			For Local l:Int = 0 To m.pass.Length-1
				Local layernode:xmlNode = maproot.AddNode("layer")
				layernode.Attribute("name").value = "collision_"+l
				layernode.Attribute("width").value = m.pass[l].wide
				layernode.Attribute("height").value = m.pass[l].high
				
				Local mapdatanode:xmlNode = layernode.AddNode("data")
				mapdatanode.Attribute("encoding").value = "csv"
				'Local gid:Int = (l*((TileSState.Tileset[m.tilesets[l]].spr.w/TileSState.Tileset[m.tilesets[l]].wide)*(TileSState.Tileset[m.tilesets[l]].spr.h/TileSState.Tileset[m.tilesets[l]].high)))+1
				Local gid:Int = 0'gidt[m.tilesets[l]]
				Local wide:Int = 0
				Local mapval:String
				For Local tm:Int = 0 To (m.pass[l].wide*m.pass[l].high)-1'm.pass[l].data.Length-1
					
								 	
					'somehow i get an array index out of range in monkey on some maps
					If tm <>  (m.pass[l].wide*m.pass[l].high)-1 Then
						If l > 0 Then
							'this gets a little weird for Tiled, don't draw the first tile on above layers..
							If m.pass[l].data[tm] = 0 Then
								mapval = mapval +"0,"
							Else
								mapval = mapval +Int(gid+m.pass[l].data[tm])+","	
							EndIf
						Else
							mapval = mapval +Int(gid+m.pass[l].data[tm])+","	
						EndIf
					Else
						If l > 0 Then
							'this gets a little weird for Tiled, don't draw the first tile on above layers..
							If m.pass[l].data[tm] = 0 Then
								mapval = mapval +"0"
							Else
								mapval = mapval +Int(gid+m.pass[l].data[tm])
							EndIf
						Else
							mapval = mapval +Int(gid+m.pass[l].data[tm])
						EndIf
					
							
					
					EndIf
				Next
				mapdatanode.value = mapval
			Next
			'DO FoeMAPS	
			For Local l:Int = 0 To m.emap.Length-1
				Local layernode:xmlNode = maproot.AddNode("layer")
				layernode.Attribute("name").value = "enemy_"+l
				layernode.Attribute("width").value = m.emap[l].wide
				layernode.Attribute("height").value = m.emap[l].high
				
				Local mapdatanode:xmlNode = layernode.AddNode("data")
				mapdatanode.Attribute("encoding").value = "csv"
				'Local gid:Int = (l*((TileSState.Tileset[m.tilesets[l]].spr.w/TileSState.Tileset[m.tilesets[l]].wide)*(TileSState.Tileset[m.tilesets[l]].spr.h/TileSState.Tileset[m.tilesets[l]].high)))+1
				Local gid:Int = 0'gidt[m.tilesets[l]]
				Local wide:Int = 0
				Local mapval:String
				For Local tm:Int = 0 To m.emap[l].data.Length-1
					
								 	
					'somehow i get an array index out of range in monkey on some maps
					If tm <> m.emap[l].data.Length-1 Then
						If l > 0 Then
							'this gets a little weird for Tiled, don't draw the first tile on above layers..
							If m.emap[l].data[tm] = 0 Then
								mapval = mapval +"0,"
							Else
								mapval = mapval +Int(gid+m.emap[l].data[tm])+","	
							EndIf
						Else
							mapval = mapval +Int(gid+m.emap[l].data[tm])+","	
						EndIf
					Else
						If l > 0 Then
							'this gets a little weird for Tiled, don't draw the first tile on above layers..
							If m.emap[l].data[tm] = 0 Then
								mapval = mapval +"0"
							Else
								mapval = mapval +Int(gid+m.emap[l].data[tm])
							EndIf
						Else
							mapval = mapval +Int(gid+m.emap[l].data[tm])
						EndIf
					
							
					
					EndIf
				Next
				mapdatanode.value = mapval
			Next
			
			
			If status <> Null Then
				SetStatusText( status,"Converting to Tiled TMX:  "+m.mapname )
				PollEvent()
			EndIf
			If FileType( filepath+"/maps" ) = 0 Then
				CreateDir(  filepath+"/maps" )
			EndIf

			mapdoc.Save(filepath+"/maps/"+"map_"+mnum+".xml")
			mnum = mnum + 1
			
		Next
		
		Local media_node:xmlNode = root.AddNode("media")

		Local palette_node:xmlNode = media_node.AddNode("palettes")
		
		Local master_p:xmlNode = palette_node.AddNode("master")
		If status <> Null Then
			SetStatusText( status,"Converting Master Palette" )
			PollEvent()
		EndIf
		
		For Local i:Int = 0 To 255
		
			If MasterPalette[i] <> Null Then
				Local palnode:xmlNode = master_p.AddNode("palette")
				Local palbank:TBank = New TBank
				Local palstream:TStream = WriteFile(palbank)
				'palstream = LittleEndianStream( palstream )
				For Local c:Int = 0 To 255
				
					'WriteInt(palstream,MasterPalette[i].col[c])
					Local col:Int[] = IntToRGBA(MasterPalette[i].col[c])
					WriteByte(palstream,col[0])
					WriteByte(palstream,col[1])
					WriteByte(palstream,col[2])


				Next
				palnode.value = TBase64.Encode(palbank,palbank.Size(),0,8)
			EndIf
		Next
		If status <> Null Then
			SetStatusText( status,"Converting Default Palettes" )
			PollEvent()
		EndIf
		
		Local game_p:xmlNode = palette_node.AddNode("game")
		Local countp:Int=0
		For Local i:Int = 0 To 255
		
			If DefPalette[i] <> Null Then
				Local palnode:xmlNode = game_p.AddNode("palette")
				palnode.Attribute("id").value = i
				Local csvpal:String
				'Local palbank:TBank = New TBank
				'Local palstream:TStream = WriteFile(palbank)
				'palstream = LittleEndianStream( palstream )

				For Local c:Int = 0 To 255
					DebugLog "COL INT: "+DefPalette[i].col[c]
					Local col:Int[] = IntToRGBA( DefPalette[i].col[c] )
					If c = 255 Then
						csvpal = csvpal+col[0]+","+col[1]+","+col[2]
					Else
						csvpal = csvpal+col[0]+","+col[1]+","+col[2]+","
					EndIf
					'WriteByte(palstream,col[0])
					'WriteByte(palstream,col[1])
					'WriteByte(palstream,col[2])
					
				
				Next
				'CloseFile(palstream)
				palnode.value = csvpal'TBase64.Encode(palbank,palbank.Size(),0,8)
				countp = countp + 1
			Else
				Continue
			EndIf
		Next
		game_p.Attribute("count").value = countp
		If status <> Null Then
			SetStatusText( status,"Converting Tilesets" )
			PollEvent()
		EndIf
		
		Local tileset_node:xmlNode = media_node.AddNode("tilesets")
		
		For Local i:Int = 0 To TileSState.numsets-1
			If status <> Null Then
				SetStatusText( status,"Processing Tileset: "+i )
				PollEvent()
			EndIf
		
			Local tset_node:xmlNode = tileset_node.AddNode("set")
			tset_node.Attribute("id").value = i
			Local spritebank:TBank = New TBank
			'Local spritestream:TStream = WriteFile(spritebank)
			Local frame_node:xmlNode = tset_node.AddNode("frame")
			
			If FileType( filepath+"/graphics" ) = 0 Then
				CreateDir(  filepath+"/graphics" )
			EndIf
			'xmldocument does not like raw data, so i was forced to compress the maptileset
			SavePixmapPNG( TileSState.Tileset[i].spr.sprite,filepath+"/graphics/"+"tileset_"+i+".png")
			
			'For Local yy:Int = 0 To TileSState.Tileset[i].spr.h-1
			'	For Local xx:Int = 0 To TileSState.Tileset[i].spr.w-1
			'		WriteInt(spritestream,ReadPixel(TileSState.Tileset[i].spr.sprite,xx,yy))'TileSState.Tileset[i].spr.palmap[xx,yy]
			'	Next
			'Next
			'CloseFile(spritestream)		
			frame_node.value ="tileset_"+i+".png"'TBase64.Encode(spritebank, spritebank.Size())
			
			
		
		Next
		
		Local backdrop_node:xmlNode = media_node.AddNode("backdrops")
		backdrop_node.Attribute("count").value = BackdropSet.numsets
		For Local i:Int = 0 To BackdropSet.numsets-1
			If status <> Null Then
				SetStatusText( status,"Processing Backdrop: "+i )
				PollEvent()
			EndIf
		
			Local bd_node:xmlNode = backdrop_node.AddNode("set")
			bd_node.Attribute("id").value = i
			Local spritebank:TBank = New TBank
			'Local spritestream:TStream = WriteFile(spritebank)
			'Local frame_node:xmlNode = bd_node.AddNode("frame")
			
			If FileType( filepath+"/graphics" ) = 0 Then
				CreateDir(  filepath+"/graphics" )
			EndIf
			If FileType( filepath+"/graphics/backdrops" ) = 0 Then
				CreateDir(  filepath+"/graphics/backdrops" )
			EndIf
			
			'xmldocument does not like raw data, so i was forced to compress the maptileset
			SavePixmapPNG( BackdropSet.Backdrop[i].spr.sprite,filepath+"/graphics/backdrops/bd_"+i+".png")
			
			
			'For Local yy:Int = 0 To TileSState.Tileset[i].spr.h-1
			'	For Local xx:Int = 0 To TileSState.Tileset[i].spr.w-1
			'		WriteInt(spritestream,ReadPixel(TileSState.Tileset[i].spr.sprite,xx,yy))'TileSState.Tileset[i].spr.palmap[xx,yy]
			'	Next
			'Next
			'CloseFile(spritestream)		
			bd_node.Attribute("source").value ="backdrops/bd_"+i+".png"'TBase64.Encode(spritebank, spritebank.Size())
			
			
		
		Next
		
				
		Local sprite_node:xmlNode = media_node.AddNode("sprites")
		
		If status <> Null Then
			SetStatusText( status,"Converting Walkabout" )
			PollEvent()
		EndIf
		
		
		For Local i:Int = 0 To WalkaboutState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("walkabout")
			walkabout_node.Attribute("frames").value = WalkaboutState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = WalkaboutState.poffset[i]
			walkabout_node.Attribute("w").value = WalkaboutState.wide
			walkabout_node.Attribute("h").value = WalkaboutState.high
			walkabout_node.Attribute("id").value = i

			Local fullset:TPixmap = CreatePixmap( WalkaboutState.wide*WalkaboutState.sets[i].Frames.Length,WalkaboutState.high,PF_RGBA8888 )
			For Local j:Int = 0 To WalkaboutState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				'spritestream = LittleEndianStream( spritestream )
				Local csvspr:String
				For Local yy:Int = 0 To WalkaboutState.high-1
					For Local xx:Int = 0 To WalkaboutState.wide-1
					
						If xx = WalkaboutState.wide-1 And yy = WalkaboutState.high-1 Then

							csvspr = csvspr + "" +WalkaboutState.sets[i].frames[j].palmap[xx,yy]
							
						Else
							csvspr = csvspr + "" +WalkaboutState.sets[i].frames[j].palmap[xx,yy]+","
						EndIf
						'Since palettes are 0-255 writebyte should be ok
						'WriteByte(spritestream,WalkaboutState.sets[i].frames[j].palmap[xx,yy])
						'DebugLog "PALMAP: xx: "+xx+" yy: "+yy+" col: "+WalkaboutState.sets[i].frames[j].palmap[xx,yy]
						'need to check for transparent pixels
						If WalkaboutState.sets[i].frames[j].palmap[xx,yy] = 0 Then
							WritePixel( fullset,(j*WalkaboutState.wide)+xx,yy,0 )
						Else
							WritePixel( fullset,(j*WalkaboutState.wide)+xx,yy,ReadPixel(WalkaboutState.sets[i].frames[j].sprite,xx,yy) )
						EndIf
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = csvspr'TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/walkabout" ) = 0 Then
					CreateDir(  filepath+"/graphics/walkabout" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/walkabout/"+i+"_"+j+".png"
				SavePixmapPNG( WalkaboutState.sets[i].frames[j].sprite,filepath+"/graphics/walkabout/"+i+"_"+j+".png" )
				

			Next
			SavePixmapPNG( fullset,filepath+"/graphics/walkabout/fullset_"+i+".png" )
			'Diddy automatically looks in graphics folder so i have to cut it from the source url
			walkabout_node.Attribute("fullset").value = "walkabout/fullset_"+i+".png"
		
		Next

		If status <> Null Then
			SetStatusText( status,"Converting Hero" )
			PollEvent()
		EndIf

		For Local i:Int = 0 To HeroState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("hero")
			walkabout_node.Attribute("frames").value = HeroState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = HeroState.poffset[i]
			walkabout_node.Attribute("w").value = HeroState.wide
			walkabout_node.Attribute("h").value = HeroState.high
			
			For Local j:Int = 0 To HeroState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To HeroState.high-1
					For Local xx:Int = 0 To HeroState.wide-1
						WriteInt(spritestream,HeroState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/hero" ) = 0 Then
					CreateDir(  filepath+"/graphics/hero" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/hero/"+i+"_"+j+".png"
				SavePixmapPNG( HeroState.sets[i].frames[j].sprite,filepath+"/graphics/hero/"+i+"_"+j+".png" )
				
			Next
		
		Next
		If status <> Null Then
			SetStatusText( status,"Converting  Small Enemy" )
			PollEvent()
		EndIf

		For Local i:Int = 0 To SmallEnemyState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("smallenemy")
			walkabout_node.Attribute("frames").value = SmallEnemyState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = SmallEnemyState.poffset[i]
			walkabout_node.Attribute("w").value = SmallEnemyState.wide
			walkabout_node.Attribute("h").value = SmallEnemyState.high
			
			For Local j:Int = 0 To SmallEnemyState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To SmallEnemyState.high-1
					For Local xx:Int = 0 To SmallEnemyState.wide-1
						WriteInt(spritestream,SmallEnemyState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/sm_enemy" ) = 0 Then
					CreateDir(  filepath+"/graphics/sm_enemy" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/sm_enemy/"+i+"_"+j+".png"
				SavePixmapPNG( SmallEnemyState.sets[i].frames[j].sprite,filepath+"/graphics/sm_enemy/"+i+"_"+j+".png" )

			Next
		
		Next

		If status <> Null Then
			SetStatusText( status,"Converting Medium Enemy" )
			PollEvent()
		EndIf

		
		For Local i:Int = 0 To MediumEnemyState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("mediumenemy")
			walkabout_node.Attribute("frames").value = MediumEnemyState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = MediumEnemyState.poffset[i]
			walkabout_node.Attribute("w").value = MediumEnemyState.wide
			walkabout_node.Attribute("h").value = MediumEnemyState.high
			
			For Local j:Int = 0 To MediumEnemyState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To MediumEnemyState.high-1
					For Local xx:Int = 0 To MediumEnemyState.wide-1
						WriteInt(spritestream,MediumEnemyState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/md_enemy" ) = 0 Then
					CreateDir(  filepath+"/graphics/md_enemy" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/md_enemy/"+i+"_"+j+".png"
				SavePixmapPNG( MediumEnemyState.sets[i].frames[j].sprite,filepath+"/graphics/md_enemy/"+i+"_"+j+".png" )
				
			Next
		
		Next

		If status <> Null Then
			SetStatusText( status,"Converting Large Enemy" )
			PollEvent()
		EndIf


		For Local i:Int = 0 To LargeEnemyState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("largeenemy")
			walkabout_node.Attribute("frames").value = LargeEnemyState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = LargeEnemyState.poffset[i]
			walkabout_node.Attribute("w").value = LargeEnemyState.wide
			walkabout_node.Attribute("h").value = LargeEnemyState.high
			
			For Local j:Int = 0 To LargeEnemyState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To LargeEnemyState.high-1
					For Local xx:Int = 0 To LargeEnemyState.wide-1
						WriteInt(spritestream,LargeEnemyState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/lg_enemy" ) = 0 Then
					CreateDir(  filepath+"/graphics/lg_enemy" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/lg_enemy/"+i+"_"+j+".png"
				SavePixmapPNG( LargeEnemyState.sets[i].frames[j].sprite,filepath+"/graphics/lg_enemy/"+i+"_"+j+".png" )
				
			Next
		
		Next

		If status <> Null Then
			SetStatusText( status,"Converting Attacks" )
			PollEvent()
		EndIf


		For Local i:Int = 0 To AttackState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("attack")
			walkabout_node.Attribute("frames").value = AttackState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = AttackState.poffset[i]
			walkabout_node.Attribute("w").value = AttackState.wide
			walkabout_node.Attribute("h").value = AttackState.high
			
			For Local j:Int = 0 To AttackState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To AttackState.high-1
					For Local xx:Int = 0 To AttackState.wide-1
						WriteInt(spritestream,AttackState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/attacks" ) = 0 Then
					CreateDir(  filepath+"/graphics/attacks" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value ="graphics/attacks/"+i+"_"+j+".png"
				SavePixmapPNG( AttackState.sets[i].frames[j].sprite,filepath+"/graphics/attacks/"+i+"_"+j+".png" )
				
			Next
		
		Next

		If status <> Null Then
			SetStatusText( status,"Converting Weapons" )
			PollEvent()
		EndIf


		For Local i:Int = 0 To WeaponState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("weapon")
			walkabout_node.Attribute("frames").value = WeaponState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = WeaponState.poffset[i]
			walkabout_node.Attribute("w").value = WeaponState.wide
			walkabout_node.Attribute("h").value = WeaponState.high
			
			For Local j:Int = 0 To WeaponState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To WeaponState.high-1
					For Local xx:Int = 0 To WeaponState.wide-1
						WriteInt(spritestream,WeaponState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/weapons" ) = 0 Then
					CreateDir(  filepath+"/graphics/weapons" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/weapons/"+i+"_"+j+".png"
				SavePixmapPNG( WeaponState.sets[i].frames[j].sprite,filepath+"/graphics/weapons/"+i+"_"+j+".png" )
				
			Next
		
		Next
		If status <> Null Then
			SetStatusText( status,"Converting Box Borders" )
			PollEvent()
		EndIf


		For Local i:Int = 0 To BoxState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("box")
			walkabout_node.Attribute("frames").value = BoxState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = BoxState.poffset[i]
			walkabout_node.Attribute("w").value = BoxState.wide
			walkabout_node.Attribute("h").value = BoxState.high
			
			For Local j:Int = 0 To BoxState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To BoxState.high-1
					For Local xx:Int = 0 To BoxState.wide-1
						WriteInt(spritestream,BoxState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/borders" ) = 0 Then
					CreateDir(  filepath+"/graphics/borders" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/borders/"+i+"_"+j+".png"
				SavePixmapPNG( BoxState.sets[i].frames[j].sprite,filepath+"/graphics/borders/"+i+"_"+j+".png" )
				
			Next
		
		Next

		If status <> Null Then
			SetStatusText( status,"Converting Portraits" )
			PollEvent()
		EndIf


		For Local i:Int = 0 To PortraitState.numsets-1
			Local walkabout_node:xmlNode = sprite_node.AddNode("portrait")
			walkabout_node.Attribute("frames").value = PortraitState.sets[i].Frames.Length
			walkabout_node.Attribute("palette").value = PortraitState.poffset[i]
			walkabout_node.Attribute("w").value = PortraitState.wide
			walkabout_node.Attribute("h").value = PortraitState.high
			
			For Local j:Int = 0 To PortraitState.sets[i].frames.Length-1
				Local frame_node:xmlNode = walkabout_node.AddNode("frame")
				Local spritebank:TBank = New TBank
				Local spritestream:TStream = WriteFile(spritebank)
				For Local yy:Int = 0 To PortraitState.high-1
					For Local xx:Int = 0 To PortraitState.wide-1
						WriteInt(spritestream,PortraitState.sets[i].frames[j].palmap[xx,yy])
					Next
				Next
				CloseFile(spritestream)		
				frame_node.value = TBase64.Encode(spritebank, spritebank.Size(),0,8)
				If FileType( filepath+"/graphics" ) = 0 Then
					CreateDir(  filepath+"/graphics" )
				EndIf
				If FileType( filepath+"/graphics/portraits" ) = 0 Then
					CreateDir(  filepath+"/graphics/portraits" )
				EndIf
				walkabout_node.Attribute("frame_"+j).value = "graphics/portraits/"+i+"_"+j+".png"
				SavePixmapPNG( PortraitState.sets[i].frames[j].sprite,filepath+"/graphics/portraits/"+i+"_"+j+".png" )
				
			Next
		
		Next

		If status <> Null Then
			SetStatusText( status,"Resaving Music" )
			PollEvent()
		EndIf
		
		Local song_node:xmlNode = media_node.AddNode("songs")
		song_node.Attribute("count").value = MusicState.numsets
		For Local i:Int = 0 To MusicState.numsets-1
			Local music_node:xmlNode = song_node.AddNode("song")
			music_node.Attribute("id").value = i
			If FileType( filepath+"/music" ) = 0 Then
				CreateDir(  filepath+"/music" )
			EndIf
			DebugLog "MOVE SONG: "+MusicState.Ssongs[i].songfile+" to "+filepath+"/music/"+StripDir(MusicState.Ssongs[i].songfile)
			CopyFile( MusicState.Ssongs[i].songfile,filepath+"/music/"+StripDir(MusicState.Ssongs[i].songfile) )			
			music_node.Attribute("source").value = StripDir(MusicState.Ssongs[i].songfile)
			
			If status <> Null Then
				SetStatusText( status,"Moving Song: "+StripDir(StripExt(MusicState.Ssongs[i].songfile)) )
				PollEvent()
			EndIf		
		
		Next

		Local sound_node:xmlNode = media_node.AddNode("sounds")
		sound_node.Attribute("count").value = SFXState.numsets
		For Local i:Int = 0 To SFXState.numsets-1
			Local sfx_node:xmlNode = sound_node.AddNode("sound")
			sfx_node.Attribute("id").value = i
			If FileType( filepath+"/sounds" ) = 0 Then
				CreateDir(  filepath+"/sounds" )
			EndIf
			DebugLog "MOVE SOUND: "+SFXState.Ssounds[i].soundfile+" to "+filepath+"/sounds/"+StripDir(SFXState.Ssounds[i].soundfile)
			CopyFile( SFXState.Ssounds[i].soundfile,filepath+"/sounds/"+StripDir(SFXState.Ssounds[i].soundfile) )			
			sfx_node.Attribute("source").value = StripDir(SFXState.Ssounds[i].soundfile)
			
			If status <> Null Then
				SetStatusText( status,"Moving Sound: "+StripDir(StripExt(SFXState.Ssounds[i].soundfile)) )
				PollEvent()
			EndIf		
		
		Next

		

		
		If status <> Null Then
			SetStatusText( status,"Saving new format. Please wait.may take a while" )
			PollEvent()
		EndIf
		'Local mybank:TBank = New TBank
		rpgfile.Save(filepath+"/myrpg.xml")	
		If status <> Null Then
			SetStatusText( status,"Finished Writing" )
			PollEvent()
		EndIf
		
		'SaveBank(mybank,filename)
	
	End Method
	

End Type
